package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.jcy.JcyPrdjConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyJcyPrjbxx;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SpcyJcyPrjbxxMapper;
import com.yuncheng.spcyApi.service.ISpcyJcyPrjbxxService;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 检查员-聘任基本信息
 * @Author: yuncheng
 * @Date: 2024-01-16
 * @Version: V1.0
 */
@Service
public class SpcyJcyPrjbxxServiceImpl extends BaseServiceImpl<SpcyJcyPrjbxxMapper, SpcyJcyPrjbxx> implements ISpcyJcyPrjbxxService {

    @Resource
    private SpcyJcyPrjbxxMapper spcyJcyPrjbxxMapper;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public Page<SpcyJcyPrjbxx> pageList(SpcyJcyPrjbxx query, String gjz, PageVo pageVo, OrderItemVo orderItemVo) {
        Page<SpcyJcyPrjbxx> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        Page<SpcyJcyPrjbxx> pageList = spcyJcyPrjbxxMapper.selectPrdjPageList(page, query, gjz, orderItemVo);
        return pageList;

        /*LambdaQueryWrapper<SpcyJcyPrjbxx> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFprwh()), SpcyJcyPrjbxx::getFprwh, query.getFprwh());
        queryWrapper.eq(StringUtils.isNotBlank(query.getFprzt()), SpcyJcyPrjbxx::getFprzt, query.getFprzt());
        queryWrapper.like(StringUtils.isNotBlank(query.getFxpjcylb()), SpcyJcyPrjbxx::getFxpjcylb, query.getFxpjcylb());

        if (StringUtils.isNotBlank(gjz)){
            queryWrapper
                    .and(f->f.like(SpcyJcyPrjbxx::getFprwh, gjz)
                            .or()
                            .like(SpcyJcyPrjbxx::getFprzt, gjz)
                            .or()
                            .like(SpcyJcyPrjbxx::getFprbzsm, gjz)
                            .or()
                            .like(SpcyJcyPrjbxx::getFxpjcylb, gjz)
                            .or()
                            .like(SpcyJcyPrjbxx::getFprsj, gjz)
                    );
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SpcyJcyPrjbxx::getCreateTime);
            queryWrapper.orderByDesc(SpcyJcyPrjbxx::getFprsj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return page(page, queryWrapper);*/
    }

    @Override
    public SpcyJcyPrjbxx addPrdj(SpcyJcyPrjbxx bean) {

        bean.setFprzt(JcyPrdjConstant.getFirstPrzt());

        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setCreateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        bean.setSysOrgCode(GetCurrentUserUtils.getCurrentUserOrgCode());
        save(bean);

        String operContent = "新增信息：";
        operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_JCY_PRDJGL,operContent,bean.getId(), "添加聘任登记", SpcyConstant.LOGS_SYS_TYPE_XZ);

        return bean;
    }

    @Override
    public void updatePrdj(SpcyJcyPrjbxx buffer) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        String id = buffer.getId();
        if (StringUtils.isEmpty(id)){
            throw new BusinessException("参数为空!");
        }
        SpcyJcyPrjbxx updateBean = getById(id);
        if (updateBean == null){
            throw new BusinessException("没有相匹配数据！");
        }
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(buffer));

        updateBean.setUpdateTime(new Date());
        updateBean.setUpdateBy(GetCurrentUserUtils.getCurrentUserAccount());
        updateById(updateBean);

        String operContent = "基本信息修改：";
        operContent += LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_JCY_PRDJGL,operContent,id,"修改聘任登记", SpcyConstant.LOGS_SYS_TYPE_XG);

    }

    @Override
    public List<String> deletePrdj(List<String> idsList) {
        if (CollectionUtil.isEmpty(idsList)){
            throw new BusinessException("参数为空！");
        }

        List<String> tyDelIdList = new ArrayList<>(); // 允许删除的数据id

        List<SpcyJcyPrjbxx> list = listByIds(idsList);
        if (CollectionUtil.isNotEmpty(list)){
            for (SpcyJcyPrjbxx bean : list){
                if (StringUtils.isNotBlank(bean.getFprzt()) && bean.getFprzt().equals(JcyPrdjConstant.PR_JHZT_PRWC)){
                    throw new BusinessException("存在已经聘任完成的数据！无法删除相关数据！");
                }

                tyDelIdList.add(bean.getId());

                removeById(bean.getId());

                String operContent = "删除信息：";
                operContent += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
                spcyLogService.addLogs(LogConstant.LOGS_JCY_PRDJGL,operContent,bean.getId(), "删除聘任登记", SpcyConstant.LOGS_SYS_TYPE_SC);
            }
        }

        return tyDelIdList;
    }

}
